/* Author: Panos Sakkos 2011 
 * Email: p.sakkos@di.uoa.gr
 */

#include "trigonal.h"
#include <iostream>

using namespace std;

/* CL implementation */

CL::CL(int size)
{
	this->size = size;
	this->values = new double[size - 1];
	
	for(int i = 0; i < size - 1; i++)
	{
		values[i] = 0;
	}
}

double CL::GetValue(int i, int j)
{
	return i == j + 1 ? values[j] : 0;
}

void CL::SetValue(int i, int j, double value)
{
	if(i == j + 1)
	{
		values[j] = value;
	}
}

/* UL implementation */

CU::CU(int size)
{
	this->size = size;
	this->values = new double[size];
	
	for(int i = 0; i < size; i++)
	{
		values[i] = 0;
	}	
}

double CU::GetValue(int i, int j)
{
	return j == i + 1 ? values[i] : 0;
}

void CU::SetValue(int i, int j, double value)
{
	if(j == i + 1)
	{
		values[i] = value;
	}
}


/* Diagonal implementation */

Diagonal::Diagonal(int size)
{
	this->size = size;
	values = new double[size];
	
	for(int i = 0; i < size; i++)
	{
		values[i] = 0;	
	}
}

double Diagonal::GetValue(int i, int j)
{
	return i == j ? values[i] : 0;
}

void Diagonal::SetValue(int i, int j, double value)
{
	if (i == j)
	{
		this->values[i] = value;
	}
}

/* Trigonal Implementation */

Trigonal::Trigonal(int size) : upper(size), diagonal(size), lower(size)
{
	this->size = size;
}

void Trigonal::SetValue(int i, int j, double value)
{
	if(i < j)
	{
		this->upper.SetValue(i, j, value);	
	}
	else if(i == j)
	{
		this->diagonal.SetValue(i, j, value);
	}
	else if(i > j)
	{
		this->lower.SetValue(i, j, value);
	}
}

double Trigonal::GetValue(int i, int j)
{
	if(i < j)
	{
		return this->upper.GetValue(i, j);	
	}
	else if(i == j)
	{
		return this->diagonal.GetValue(i, j);
	}
	else
	{
		return this->lower.GetValue(i, j);
	}
}

void Trigonal::ReadInput(void)
{
	double input;
	for(int i = 0; i < this->size; i++)
	{
		for(int j = 0; j < this->size; j ++)
		{
			if(i == j + 1)
			{
				cout << "Please, type value for " << i << ", " << j << ": \n";
				
				cin >> input;
				lower.SetValue(i, j, input);
			}
			else if(i == j)
			{
				cout << "Please, type value for " << i << ", " << j << ": \n";
				
				cin >> input;
				diagonal.SetValue(i , j, input);
			}
			else if(j == i + 1)
			{
				cout << "Please, type value for " << i << ", " << j << ": \n";
				
				cin >> input;
				upper.SetValue(i, j, input);
			}
		}
	}
}